﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace OnlineManagement.Web.webs
{
    public partial class SowEventMsg : BasePage
    {
        private string bitId;
        protected void Page_Load(object sender, EventArgs e)
        {
            int hoggeryID = HoggeryId;
            if (hoggeryID <= 0)
            {
                panInfo.Visible = false;
                lbMsg.Text = "没有查询到相关猪管理日志信息";
                return;
            }
            repeaterShow.ItemDataBound += new RepeaterItemEventHandler(repeaterShow_ItemDataBound);
            if(!Page.IsPostBack){
                lbMsg.Text = string.Empty;
                string str = Request.QueryString["sowId"] == null ?
                  string.Empty : Request.QueryString["sowId"].ToString();
                bitId = Request.QueryString["bitId"] == null ?
                   string.Empty : Request.QueryString["bitId"].ToString();
                if (Request.Cookies.Get("logSowId") != null && Request.Cookies.Get("logBitId") != null)
                {
                    str = Request.Cookies.Get("logSowId").Value;
                    bitId = Request.Cookies.Get("logBitId").Value;
                }
               
                hfBitID.Value = bitId;
                Guid sowId;
                if (!Guid.TryParse(str, out sowId))
                {
                    panInfo.Visible = false;
                    lbMsg.Text = "没有查询到相关猪管理日志信息";
                }
                else
                {
                    panInfo.Visible = true;
                    BindRepeater(sowId);
                }
            }
        }

        private void BindRepeater(Guid sowId)
        {
            try
            {
               BLL.Dal.Hoggery bllHog = new BLL.Dal.Hoggery();
               System.Data.DataSet ds= bllHog.GetHoggeryMgrLog(sowId);
               if (ds != null && ds.Tables.Count > 0)
               {
                   repeaterShow.DataSource = ds;
                   repeaterShow.DataBind();
               }
               else {
                   panInfo.Visible = false;
                   lbMsg.Text = "没有查询到相关猪管理日志信息";
               }
            }catch(Exception ex){
                panInfo.Visible = false;
                lbMsg.Text = Common.PubMsg.ServerError;
            }
        }

        void repeaterShow_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item 
                || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lbSowBitID = e.Item.FindControl("lbSowBitID") as Label;
                lbSowBitID.Text = hfBitID.Value;
            }
        }

     


        //private int GetHoggeryID()
        //{
        //    int hoggeryId = 0;
        //    if (!HttpContext.Current.User.Identity.IsAuthenticated)
        //    {
        //        return hoggeryId;
        //    }
        //    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
        //    string hoggeryIdStr = id.Ticket.UserData;
        //    int.TryParse(hoggeryIdStr, out hoggeryId);
          
        //    return hoggeryId;
        //}
    }
}